package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.Login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 确认订单
     *
     * @param tradeVo
     * @return
     */
    @TsLogin
    @Operation(summary = "确认订单")
    @PostMapping("/trade")
    public Result trade(@RequestBody TradeVo tradeVo) {

        Long userId = AuthContextHolder.getUserId();

        OrderInfoVo orderInfoVo = orderInfoService.trade(userId, tradeVo);

        return Result.ok(orderInfoVo);
    }

    /**
     * 提交订单
     *
     * @param orderInfoVo
     * @return
     */
    @TsLogin
    @Operation(summary = "提交订单")
    @PostMapping("/submitOrder")
    public Result<Map<String, Object>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        String orderNo = orderInfoService.submitOrder(userId, orderInfoVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return Result.ok(map);
    }
    /**
     * 根据订单号获取订单信息
     */
    @TsLogin
    @Operation(summary = "根据订单号获取订单信息")
    @GetMapping("/getOrderInfo/{tradeNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String tradeNo){
        OrderInfo  orderInfoResult=orderInfoService.getOrderInfo(tradeNo);
        return Result.ok(orderInfoResult);
    }

    /**
     * 查看我的订单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @TsLogin
    @Operation(summary = "查看我的订单")
    @GetMapping("/findUserPage/{pageNum}/{pageSize}")
    public Result<IPage<OrderInfo>>  findUserPage(@PathVariable Long pageNum,
                                                  @PathVariable Long pageSize,
                                                  HttpServletRequest request){
        //从请求头中获取指定的orderStatus请求体数据 -订单状态
        String orderStatus = request.getParameter("orderStatus");
        Page<OrderInfo> orderInfoPage = new Page<>(pageNum,pageSize);
        Long userId = AuthContextHolder.getUserId();
        IPage<OrderInfo> orderInfoResult=orderInfoService.findUserPage(orderInfoPage,userId,orderStatus);
        return Result.ok(orderInfoResult);
    }
}

